Ext.define('Admin.view.part.PartViewController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.partViewController',
    	
    search: function(view, recIndex, cellIndex, item, e, record) {
		
		
		var store = Ext.getCmp('partGridPanel').getStore();
		
		Ext.apply(store.proxy.extraParams, {partName:this.lookupReference('partSearchPartName').getValue()});
		
		store.load({params:{start:0, limit:25, page:1}});
		
    } ,openAddWindow: function(view, recIndex, cellIndex, item, e) {
      		    	var cfg = Ext.apply({
                xtype: 'partAddWindow',
                items: [Ext.apply({xtype: 'partAddForm'})]
		});
		var win = Ext.create(cfg);
    },submitAddForm: function(btn) {
      		var form = btn.up('form');
			form.getForm().submit({       
				url:'part/saveOrUpdate',
				//waitMsg: '正在上传，请耐心等待....',
				success: function(view, action){    
					Ext.Msg.alert('提示', action.result.msg,function(){
						btn.up('window').close();
						//form.getViewModel().getStore('partStore').reload();
	                    Ext.getCmp('partGridPanel').getStore().reload();
					});       
				},       
				failure: function(view, action){
					Ext.Msg.alert('提示', action.result.msg);
				}
			});
    }
    ,openEditWindow: function(view, recIndex, cellIndex, item, e, record) {
    	var cfg = Ext.apply({
                xtype: 'partAddWindow',
                items: [Ext.apply({xtype: 'partEditForm'})]
		});
		var win = Ext.create(cfg);
		
		win.down("form").getForm().loadRecord(record);//加载修改数据到Form
		
      	
    }
     ,submitEditForm: function(btn) {
      		var form = btn.up('form');
			form.getForm().submit({       
				url:'part/saveOrUpdate',
				//waitMsg: '正在上传，请耐心等待....',
				success: function(view, action){    
					Ext.Msg.alert('提示', action.result.msg,function(){
						btn.up('window').close();
						//form.getViewModel().getStore('partStore').reload();
	                    Ext.getCmp('partGridPanel').getStore().reload();
					});       
				},       
				failure: function(view, action){
					Ext.Msg.alert('提示', action.result.msg);
				}
			});
    }
    ,deletePart: function(view, recIndex, cellIndex, item, e, record) {
      		Ext.MessageBox.confirm('提示', '确定要进行删除操作吗？数据将无法还原！',
      			function(btn, text){
                	if(btn=='yes'){
                			Ext.Ajax.request({ 
								url : 'part/delete', 
								method : 'post', 
								params : { 
									partId :record.get('partId')
								}, 
								success: function(response, options) {
					                var json = Ext.util.JSON.decode(response.responseText);
						            if(json.success){
						            	Ext.Msg.alert('系统提示', json.msg, function() {
						                    view.getStore().reload();
						                });
							        }else{
							        	 Ext.Msg.alert('系统提示', json.msg);
							        }
					            }
							});
               		}
            	}
            , this);
    },
    deleteParts: function(btn) {
      	var grid = btn.up('gridpanel');
		var selModel = grid.getSelectionModel();
        if (selModel.hasSelection()) {
            Ext.Msg.confirm("警告", "确定要删除吗？", function (button) {
                if (button == "yes") {
                    var rows = selModel.getSelection();
                    var selectIds = []; //要删除的id
                    Ext.each(rows, function (row) {
                        selectIds.push(row.data.id);
                    });
                  	Ext.Ajax.request({ 
						url : 'part/deleteParts', 
						method : 'post', 
						params : { 
							//ids[] :selectIds
							ids :selectIds
						}, 
						success: function(response, options) {
			                var json = Ext.util.JSON.decode(response.responseText);
				            if(json.success){
				            	Ext.Msg.alert('操作成功', json.msg, function() {
				                    grid.getStore().reload();
				                });
					        }else{
					        	 Ext.Msg.alert('操作失败', json.msg);
					        }
			            }
					});

                }
            });
        }
        else {
            Ext.Msg.alert("错误", "没有任何行被选中，无法进行删除操作！");
        }
    },
     partTip:function( view, record, item, index, e, eOpts ){
      if (view.tip == null) {  //这块判断很重要，不能每次都创建个tooltip，要不显示会有问题。
                view.tip = Ext.create('Ext.tip.ToolTip', {
                    // The overall target element.
                    target: view.el,
                    // Each grid row causes its own separate show and hide.
                    delegate: view.itemSelector,
                    // Moving within the row should not hide the tip.
                    //  trackMouse: false,
                    // Render immediately so that tip.body can be referenced prior to the first show.
                    renderTo: Ext.getBody()

                });
            };
           
             view.el.clean();
               emptip='';
               num=0;
              Ext.Ajax.request({  
                url : "part/findEmployee",  
                params : {  
                     partId:record.data.partId
                },  
                method : "POST",  
                success : function(response) {  
                    var respText = Ext.util.JSON.decode(response.responseText); 
                   
                     
                    if (respText) {  
                       for(var i=0;i<Object.keys(respText).length;i++) {
                       // console.log(respText[i].post.postName);

                        // emptip='aaa';
                        if(respText[i].post.postName.indexOf('部长')>0) {
                          emptip=emptip+respText[i].post.postName+':'+respText[i].empName+'<br>';
                        }else{
                          num+=1;
                       }


                       }
                    }
                    if(emptip=='')
                     emptip='部长:暂无<br>';
                    view.tip.update(emptip+'其他员工:'+num+'人'); 
                },  
                failure : function(response) {  
                    Ext.Msg.alert('提示',  
                            "操作失败！！！");  
                }  
            });  
  
             
   }
    
});